package com.qf.ssm.controller;

import com.github.pagehelper.PageInfo;
import com.qf.ssm.po.Store;
import com.qf.ssm.qo.StoreEmpQo;
import com.qf.ssm.qo.StoreQo;
import com.qf.ssm.result.ResponseResult;
import com.qf.ssm.service.StoreService;
import com.qf.ssm.utlis.ImageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/store")
public class StoreController {
    @Autowired
    StoreService storeService;

    @PostMapping("/addStore")
    public ResponseResult<Integer> addStore(@RequestBody Store store){
        return ResponseResult.success(storeService.addStore(store));
    }

    @PostMapping("/list/{page}/{size}")
    public ResponseResult<PageInfo<Store>> list(@PathVariable int page,@PathVariable int size,@RequestBody StoreQo storeQo){
        ResponseResult<PageInfo<Store>> result = null;
        return ResponseResult.success(storeService.list(page, size, storeQo));
    }

    @PostMapping("/del")
    public ResponseResult<Integer> delStore(@RequestParam("storeName") String storeName){
        return ResponseResult.success(storeService.deleteStoreByName(storeName));
    }

    @PostMapping("/update")
    public ResponseResult<Integer> modifyMsg(@RequestBody Store store){
        return ResponseResult.success(storeService.modification(store));
    }

    @PostMapping("/searchEmp")
    public ResponseResult<List<StoreEmpQo>> searchEmp(@RequestParam("storeName") String storeName){
        return ResponseResult.success(storeService.searchEmp(storeName));
    }


    @PostMapping("/image/upload")
    public String upload() {
//        String image = storeService.getImage("北京市朝阳区阜通东大街6号");
        ImageUtils.getImage("北京市朝阳区阜通东大街6号");
        return "1";
    }
    //public ResponseResult<String> upload(@RequestParam("location") String location, HttpServletRequest request) {
        //String originalFilename = pic.getOriginalFilename();
//        BufferedImage bufferedImage = ImageIO.read(new FileInputStream(storeService.getImage(location)););

//        File file = new File(image);

//        FileInputStream file = new FileInputStream(storeService.getImage(location));

//        MultipartFile pic = new MockMultipartFile(file.getName(),new FileInputStream(file));
//        String originalFilename = pic.getOriginalFilename();
//        String realPath=request.getServletContext().getRealPath("WEB-INF/upload");
//
//        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
//        String fileName = UUID.randomUUID().toString()+substring;
//        File dir = new File(realPath);
//        if (!dir.exists()){
//            dir.mkdir();
//        }
//        try {
//            pic.transferTo(new File(realPath+fileName));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return ResponseResult.success(fileName);
//        return ResponseResult.success(image);}


    @GetMapping("/image/download")
    public void download(String location, HttpServletResponse response,HttpServletRequest request){
        try {
            String realPath=request.getServletContext().getRealPath("WEB-INF/upload");
            //获取图片文件
            FileInputStream inputStream = new FileInputStream(new File(realPath+location));
            //输出流
            ServletOutputStream outputStream = response.getOutputStream();

            response.setContentType("image/jpeg");

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }
            outputStream.close();
            inputStream.close();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
